package com.freeSoft;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Properties;

public class CreateService {
	private String tableName;
	private Properties prop;
	
	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public Properties getProp() {
		return prop;
	}
	
	public void setProp(Properties prop) {
		this.prop = prop;
	}
	
	public void generateServiceInterface(){
		String servicePath = prop.getProperty(Util.PROP_SERVICE_PATH);
		String beanPath = prop.getProperty(Util.PROP_BEAN_PATH);
		String outputDir = Util.getOutputDir(prop);
		String projactName = prop.getProperty(Util.PROJECT_NAME);
		String absolutePath =outputDir+ projactName + File.separator+ projactName+ Util.CORE_NAME +File.separator+ "src"+File.separator+"main"+File.separator+"java"+File.separator+ Util.javaPackToFilePath(servicePath);
		File f = new File(absolutePath);
		if ( !f.exists()){
			f.mkdirs();
		}
		String beanName = tableName.substring(0, 1).toUpperCase()+tableName.substring(1, tableName.length());
		beanName = Util.changeName(beanName);
 
		File file = new File(absolutePath+File.separator+beanName+"Dao.java");
		try {
			PrintWriter out	   = new PrintWriter(new BufferedWriter( new FileWriter(file)));
			out.println("package "+servicePath +";\r\n");
			out.println();
			out.println("import java.util.List;");
			out.println();
//			String beanPath = Util.javaPackToFilePath( prop.getProperty(Util.PROP_BEAN_PATH) );
			out.println("import "+beanPath+"."+beanName+";");
			out.println("public interface "+ beanName +"Dao { ");
			out.println("    public int delete"+beanName+"(int keValue);");
			out.println("    public int update"+beanName+"("+beanName+" bean);");
			out.println("    public int insert"+beanName+"("+beanName+" bean );");
			out.println("    public List<" + beanName+"> get"+beanName+"ById(int keyValue);");
			out.println("    public List<"+beanName+"> getAll"+beanName+"();");
			out.println("}");
			out.flush();
			out.close();
		}catch(Exception e){
			
		}
	}
}
